package com.juqimiao.raccoon.commodity.tunnel.database;

import com.juqimiao.raccoon.commodity.tunnel.database.dataobject.CategoryGroupDO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface CategoryGroupTunnelI {

    @Insert("INSERT INTO category_group " +
            "(`name`, " +
            "spell, " +
            "group_type, " +
            "scenario, " +
            "publish, " +
            "authorized, " +
            "submit_id, " +
            "sort_order, " +
            "status, " +
            "create_date_time) " +
            "VALUES " +
            "(#{name}, " +
            "#{spell}, " +
            "#{groupType}, " +
            "#{scenario}, " +
            "#{publish}, " +
            "#{authorized}, " +
            "#{submitId}, " +
            "#{sortOrder}, " +
            "#{status}, " +
            "#{createDateTime})  ")
    int insert(CategoryGroupDO categoryGroupDO);

    @Update({"<script>",
            "<foreach collection='categoryGroupDOS' item='item' index='index' separator=';'>",
            "UPDATE category_group " +
            "SET " +
            "`name`=#{item.name}," +
            "spell=#{item.spell}," +
            "group_type=#{item.groupType}," +
            "scenario=#{item.scenario}," +
            "authorized=#{item.authorized}," +
            "submit_id=#{item.submitId}," +
            "sort_order=#{item.sortOrder}," +
            "publish=#{item.publish}," +
            "`status`=#{item.status} " +
            "WHERE id=#{item.id}",
            "</foreach>",
            "</script>"})
    int batchUpdate(@Param("categoryGroupDOS") List<CategoryGroupDO> categoryGroupDOs);

    @Update({"<script>",
            "<foreach collection='categoryGroupDOS' item='item' separator=';'>",
            "UPDATE category_group " +
                    "SET " +
                    "sort_order=#{item.sortOrder} " +
                    "WHERE id=#{item.id}",
            "</foreach>",
            "</script>"})
    int batchUpdateSortOrder(@Param("categoryGroupDOS") List<CategoryGroupDO> categoryGroupDOS);

    @Select("SELECT id, " +
            "`name`, " +
            "spell, " +
            "group_type AS groupType, " +
            "scenario, " +
            "publish, " +
            "authorized AS authorized, " +
            "submit_id AS submitId, " +
            "sort_order AS sortOrder, " +
            "create_date_time AS createDateTime, " +
            "update_data_time AS updateDateTime " +
            "FROM " +
            "category_group " +
            "WHERE " +
            "group_type=#{groupType} " +
            "AND scenario=#{scenario} " +
            "AND publish=#{publish} " +
            "ORDER BY sort_order ${sortOrder}")
    List<CategoryGroupDO> list(@Param("groupType") String groupType,
                               @Param("scenario") String scenario,
                               @Param("publish") Boolean publish,
                               @Param("sortOrder") String sortOrder);

    @Select({"<script>",
            "SELECT id AS id, " +
            "`name`, " +
            "spell, " +
            "group_type AS groupType, " +
            "scenario, " +
            "publish, " +
            "authorized AS authorized, " +
            "submit_id AS submitId, " +
            "sort_order AS sortOrder, " +
            "status, " +
            "create_date_time AS createDateTime, " +
            "update_data_time AS updateDateTime " +
            "FROM " +
            "category_group " +
            "WHERE " +
            "id IN " +
            "<foreach collection='ids' item='id' index='index' open='(' close=')' separator=','>" +
            "#{id}" +
            "</foreach>",
            "</script>"})
    List<CategoryGroupDO> getMany(@Param("ids") String[] ids);

    @Select("SELECT id AS id, " +
            "`name`, " +
            "spell, " +
            "group_type AS groupType, " +
            "scenario, " +
            "publish, " +
            "authorized AS authorized, " +
            "submit_id AS submitId, " +
            "sort_order AS sortOrder, " +
            "status, " +
            "create_date_time AS createDateTime, " +
            "update_data_time AS updateDateTime " +
            "FROM " +
            "category_group " +
            "WHERE " +
            "id=#{id} ")
    CategoryGroupDO getById(@Param("id") String id);

    @Select("SELECT id AS id, " +
            "`name`, " +
            "spell, " +
            "group_type AS groupType, " +
            "scenario, " +
            "publish, " +
            "authorized AS authorized, " +
            "submit_id AS submitId, " +
            "sort_order AS sortOrder, " +
            "create_date_time AS createDateTime, " +
            "update_data_time AS updateDateTime " +
            "FROM " +
            "category_group " +
            "WHERE " +
            "`name`=#{name} AND" +
            " group_type=#{groupType} AND" +
            " scenario=#{scenario}")
    CategoryGroupDO getByName(@Param("name") String name, @Param("groupType") String groupType,
                              @Param("scenario") String scenario);
}
